"""
将模型注册到Django自带的管理员平台
"""
import inspect
import sys

from django.contrib import admin

from polls import models
from polls.models import Subject, Teacher


class SubjectModelAdmin(admin.ModelAdmin):
    list_display = ('no', 'name', 'is_hot')
    ordering = ('no', )


class TeacherModelAdmin(admin.ModelAdmin):
    list_display = ('no', 'name', 'sex', 'birth', 'good_count', 'bad_count', 'subject')
    ordering = ('no', )
    search_fields = ('name', )
    list_per_page = 10


# 获取当前模块对应的对象
curr_module = sys.modules[__name__]
# 通过Python反射机制获取models模块中所有的类
cls_members = inspect.getmembers(models, inspect.isclass)
# 循环遍历所有模型类注册对应的模型管理类
for cls_name, cls in cls_members:
    # 根据模型的名字动态获取模型管理类
    model_admin_cls = getattr(curr_module, f'{cls_name}ModelAdmin', None)
    if model_admin_cls:
        admin.site.register(cls, model_admin_cls)
    else:
        admin.site.register(cls)

# admin.site.register(Subject, SubjectModelAdmin)
# admin.site.register(Teacher, TeacherModelAdmin)
